package com.jspxcms.core.web.fore;

import com.jspxcms.common.util.JsonMapper;
import com.jspxcms.core.domain.Area;
import com.jspxcms.core.service.AreaService;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by yuanyue on 14-8-16.
 */
@Controller
public class AreaController {

    @Autowired
    private AreaService areaService;

    @RequestMapping("/area/findby{pid}.jspx")
    public void findByPid(@PathVariable Integer pid, HttpServletResponse response) {
        List<Area> areas = areaService.findByPid(pid);
        if (areas == null) {
            areas = new ArrayList<Area>();
        }
        Area defaultArea = new Area();
        defaultArea.setId(-1);
        defaultArea.setName("请选择");
        defaultArea.setPid(0);
        areas.add(0, defaultArea);
        String json = new JsonMapper().toJson(areas);
        try {
            response.setContentType("text/json;charset=UTF-8");
            response.setCharacterEncoding("utf-8");
            response.getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
